package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.SystemDictionaryQueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String List(Model model, @ModelAttribute("qo") SystemDictionaryQueryObject qo){
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);//查出数据
        model.addAttribute("pageInfo",pageInfo);
        return "systemDictionary/list";
    }

    //（异步方式）
    //获取树状结构数据(JSON格式)
    @RequestMapping("/treeData1")
    @ResponseBody
    public List<SystemDictionary> treeData1(){
        //逻辑放在业务层
        return systemDictionaryService.queryByParentId(1L);
    }

    //（异步方式）
    //获取树状结构数据(JSON格式)
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map> treeData(){
        //逻辑放在业务层
        return systemDictionaryService.queryTreeData();
    }

    //预约单二级联动业务
    @RequestMapping("/queryByParentId")
    @ResponseBody
    public List<SystemDictionary> queryByParentId(Long parentId){
        //逻辑放在业务层
        return systemDictionaryService.queryByParentId(parentId);
    }


    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null) {
            systemDictionaryService.delete(id);
        }
        return "redirect:/systemDictionary/list";  //localhost/systemDictionary/list
    }

   /* @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionary systemDictionary){
        if (systemDictionary.getId()==null) {
            systemDictionaryService.save(systemDictionary);
        }else{
            systemDictionaryService.update(systemDictionary);
        }
        return "redirect:/systemDictionary/list";  //localhost/systemDictionary/list
    }*/
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary){
        if (systemDictionary.getId()==null) {
            systemDictionaryService.save(systemDictionary);
        }else{
            systemDictionaryService.update(systemDictionary);
        }
        return new JsonResult(true,"添加/成功");
    }
}
